package com.yuandian.middle.platform.service.impl;

import com.yuandian.api.middle_platform.po.AbilityPojo;
import com.yuandian.api.middle_platform.po.AbilityServerPojo;
import com.yuandian.api.middle_platform.po.OrgAbilityParam;
import com.yuandian.api.middle_platform.vo.AbilityVo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.middle.platform.enums.MpOutputModeEnum;
import com.yuandian.middle.platform.enums.MpServiceInvocationEnum;
import com.yuandian.middle.platform.mapper.AbilityServerMapper;
import com.yuandian.middle.platform.service.IAbilityServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * 说明：能力服务查询接口实现
 * @Author tianlr
 * @Date 2023/5/18 10:35
 */

@Service
public class AbilityServerServiceImpl implements IAbilityServerService {

    @Autowired
    private AbilityServerMapper abilityServerMapper;

    /**
     * 根据条件（单位单位、能力编码）查询能力服务
     * @return
     */
    @Override
    public R selectAbilityServer(OrgAbilityParam param) {
        //查询单位下对应的能力
        List<AbilityPojo> abilityList = abilityServerMapper.selectAbility(param);
        //查询所有的能力服务
        List<AbilityServerPojo> abilityServerList = abilityServerMapper.selectAbilityServer();

        //合并能力和服务
        List<AbilityVo> list = new ArrayList<AbilityVo>();
        AbilityVo abilityVo = null;
        if (abilityList != null && abilityList.size() > 0) {
            List<AbilityServerPojo> subList = null;
            String serverMode = "";
            for(AbilityPojo aPojo : abilityList) {
                serverMode = "";
                abilityVo = new AbilityVo();
                abilityVo.setId(aPojo.getId());
                abilityVo.setAbilityCode(aPojo.getAbilityCode());
                abilityVo.setAbilityName(aPojo.getAbilityName());
                abilityVo.setLabel(aPojo.getAbilityName());
                abilityVo.setValue(aPojo.getAbilityCode());
                abilityVo.setOutputModeCode(aPojo.getOutputModeCode());
                abilityVo.setOutputModeCodeName(MpOutputModeEnum.getItemValue(aPojo.getOutputModeCode()));
                if (abilityServerList != null && abilityServerList.size() > 0) {
                    for (AbilityServerPojo bPojo : abilityServerList) {
                        if (aPojo.getId() == bPojo.getAbilityId()) {
                            subList = abilityVo.getServerList();
                            if (subList == null) {
                                subList = new ArrayList<>();
                                abilityVo.setServerList(subList);
                            }
                            bPojo.setInvocationModeName(MpServiceInvocationEnum.getItemValue(bPojo.getInvocationMode()));
                            if ("".equals(serverMode)){
                                serverMode = bPojo.getInvocationModeName();
                            } else {
                                if (serverMode.indexOf(bPojo.getInvocationModeName()) == -1) {
                                    serverMode = serverMode + "、" + bPojo.getInvocationModeName();
                                }
                            }
                            bPojo.setLabel(bPojo.getServiceName());
                            bPojo.setValue(bPojo.getServiceCode());
                            subList.add(bPojo);
                        }
                    }
                }
                abilityVo.setOutputModeCodeName(abilityVo.getOutputModeCodeName() + "（" + serverMode +  "）");
                list.add(abilityVo);
            }
        }
        return R.ok(list);
    }
}
